<template>
  <el-dialog title="添加班组" v-model="visible" width="450px" height="75vh" destroy-on-close @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" ref="FormHandle" label-width="100px">
      <el-form-item label="班组名称" prop="name">
        <el-input v-model="form.name" clearable/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" :loading="saving" @click="onSubmit()">立即添加</el-button>
    </template>
  </el-dialog>
</template>

<script>

export default {
  data() {
    return {
      project: {},
      saving: false,
      visible: false,
      form: {
        id: null
      },
      rules: {
        name: [
          {required: true, message: '请输入班组名称'}
        ]
      },
    }
  },
  mounted() {
  },
  methods: {
    onSubmit() {
      this.$refs.FormHandle.validate(async (valid) => {
        if (valid) {
          this.saving = true;
          let res = await this.$API.project.team.create.post(this.project.id, this.form);
          this.saving = false;
          if (res.success) {
            this.$message.success("保存成功");
            this.$emit('success');
            this.visible = false;
          } else {
            this.$alert(res.message, "提示", {type: 'error'})
          }
        }
      })
    },
    open() {
      this.visible = true;
      return this
    },
    data(data) {
      Object.assign(this.project, data);
      return this;
    },
  }
}
</script>

<style lang="less" scoped>
.muzi {
  width: 75vw;
  height: 100%;
  margin: 45px auto;
}
</style>